package controller;

import com.alibaba.fastjson.JSONObject;
import entity.CurriculumList;
import org.apache.http.entity.ContentType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import service.CurriculumListService;
import utils.MultipartFileDto;
import utils.PageObject;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;

/**
 * (CurriculumList)表控制层
 * 课程管理controller层,对表的操作
 *
 * @author makejava
 * @since 2021-12-13 16:02:32
 */
@RestController
@RequestMapping("/curriculumList")
public class CurriculumListController {
    /**
     * 服务对象
     */
    @Resource
    private CurriculumListService curriculumListService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public CurriculumList selectOne(Integer id) {
        return this.curriculumListService.queryById(id);
    }

    /**
     * 查询所有数据并返回数据并前段页面展示使用
     */
    @ResponseBody
    @RequestMapping("/selectAllbylimit")
    @CrossOrigin
    public PageObject selectall(@RequestBody JSONObject object) {
        System.out.println(object.toString());
        PageObject pageObject = curriculumListService.selectAll(object);
        System.out.println(pageObject.toString());
        return pageObject;
    }

    @RequestMapping("/delete")
    @CrossOrigin
    public boolean delete(@RequestBody JSONObject insertobj){
        return  curriculumListService.deleteById(insertobj.getInteger("pkId"));

    }


    /**
     * 新增课程管理信息接口
     */

    @RequestMapping("/insertcurriculum")
    @CrossOrigin
    public JSONObject insertinterface(@RequestBody JSONObject insertobj) {
        JSONObject insert = curriculumListService.insert(insertobj);
        return insert;
    }

    @ResponseBody
    @RequestMapping("/updatecurriculum")
    public JSONObject updatecurriculum(@RequestBody JSONObject updateobj) {
        return curriculumListService.update(updateobj);
    }

    @RequestMapping("/excelimport")
    public JSONObject excelimport(@RequestBody JSONObject fileobj) throws Exception {
        System.out.println("=========" + fileobj.toString());
        String file = fileobj.getString("file");
        System.out.println(file);
        File excelfile = new File(file);
        FileInputStream fileInputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MultipartFileDto(excelfile.getName(), excelfile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        JSONObject excelimport = curriculumListService.excelimport(multipartFile);
        return excelimport;
    }
}
